import { defineStore } from 'pinia';

export const useCityStore = defineStore('city', {
    state: () => ({
        username: '',
        cityName: '',
        cityCode: '',
    }),

    getters: {
        getUsername: (state) => state.username,
        getCityName: (state) => state.cityName,
        getCityCode: (state) => state.cityCode,
    },

    actions: {
        setUsername(username) {
            this.username = username;
        },

        setCityName(name) {
            this.cityName = name;
        },

        setCityCode(code) {
            this.cityCode = code;
        },

        // 登录后设置市名
        setCityAfterLogin(cityName) {
            this.cityName = cityName;
            // 可以选择持久化存储到localStorage
            localStorage.setItem('cityName', cityName);
        },

        // 从localStorage加载市名
        loadCityFromStorage() {
            const savedCity = localStorage.getItem('cityName');
            if (savedCity) {
                this.cityName = savedCity;
            }
        },
    },
});
